﻿using UnityEngine;

namespace PP.Basic
{
    /// <summary>
    /// UI层级结，单例，随场景切换而销毁
    /// 若场景中已存在则使用，否则从资源中创建。
    /// </summary>
    [AddComponentMenu("")] // Make it be hidden from the Add Component menu
    public class UIHierarchy : UnitySingletonWithCreateMethod<UIHierarchy>
    {
        public const string PrefabPath = "Assets/Production/Basic/Res/Prefab/UI/Hierarchy/UIHierarchy.prefab";

        private const string Tag = nameof(UIHierarchy);

        // Public Properties Begin
        public static Camera MainCamera => GlobalHierarchy.MainCamera;

        /// <summary>
        /// 缓存对象层
        /// 用于放置可重复使用的UI对象，如Toast，消息对话框等。
        /// </summary>
        public static RectTransform PoolLayer => (RectTransform)Instance?._poolTrans;
        /// <summary>
        /// 模板层
        /// 用于放置模板界面，如Toast模板、消息对话模板等。
        /// </summary>
        public static RectTransform TemplateLayer => (RectTransform)Instance?._templateTrans;
        /// <summary>
        /// 背景层
        /// 用于放置全局背景图片，背景特效等。
        /// </summary>
        public static RectTransform BackgroundLayer => (RectTransform)Instance?._backgroundTrans;
        /// <summary>
        /// 内容底层
        /// 用于显示较低层级的内容，比如全屏界面，从而确保其它弹窗或新创建的内容显示在它的上面。
        /// </summary>
        public static RectTransform BottomLayer => (RectTransform)Instance?._bottomTrans;
        /// <summary>
        /// 常规内容层
        /// 用于放置内容界面，如主界面、功能界面、游戏界面等。
        /// </summary>
        public static RectTransform ContentLayer => (RectTransform)Instance?._contentTrans;
        /// <summary>
        /// 弹窗层
        /// 用于放置弹窗界面，如提示框、确认框等。
        /// </summary>
        public static RectTransform PopupLayer => (RectTransform)Instance?._popupTrans;
        /// <summary>
        /// 遮挡层
        /// 用于放置遮挡界面，如加载界面、等待界面等。
        /// </summary>
        public static RectTransform OverlayLayer => (RectTransform)Instance?._overlayTrans;
        /// <summary>
        /// 前景层
        /// 用于放置需要显示在最前端的内容。
        /// </summary>
        public static RectTransform ForegroundLayer => (RectTransform)Instance?._foregroundTrans;

        public static Transform PoolTrans => Instance?._poolTrans;
        public static Transform TemplateTrans => Instance?._templateTrans;
        public static Transform BackgroundTrans => Instance?._backgroundTrans;
        public static Transform BottomTrans => Instance?._bottomTrans;
        public static Transform ContentTrans => Instance?._contentTrans;
        public static Transform PopupTrans => Instance?._popupTrans;
        public static Transform OverlayTrans => Instance?._overlayTrans;
        public static Transform ForegroundTrans => Instance?._foregroundTrans;
        /// <summary>
        /// 3D空间世界根节点
        /// </summary>
        public static Transform WorldTrans => Instance?._worldTrans;
        // Public Properties End

        // Private Variables Begin
        // Private Variables End

        // Public Static Methods Begin

        /// <summary>
        /// 当牌局结束时清理牌桌上的内容
        /// </summary>
        public static void ResetByRoomOver()
        {
            ContentLayer.DisposeChildren(true, true);
            PopupLayer.DisposeChildren(true, true);
            OverlayLayer.DisposeChildren(true, true);
        }


        /// <summary>
        /// Create a GameObject used to hold pooled objects.
        /// </summary>
        /// <returns></returns>
        public static GameObject CreatePoolContainer(string name = null)
        {
            var go = new GameObject(name);
            var rectTransform = go.AddComponent<RectTransform>();
            rectTransform.SetParent(PoolLayer, false);
            rectTransform.anchorMin = Vector2.zero;
            rectTransform.anchorMax = Vector2.one;
            rectTransform.offsetMin = Vector2.zero;
            rectTransform.offsetMax = Vector2.zero;
            return go;
        }
        // Public Static Methods End

        // Public Methods Begin
        // Public Methods End

        // Lifecycle Methods Begin
        protected override void OnInit()
        {
            InitHier();
            InitPart();
        }

        private void InitPart()
        {
            InitCanvas();
            SetSafeArea();
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();
            Debug.Log($"{Tag}.OnDestroy");
        }
        // Lifecycle Methods End

        // Hier Components Begin        
        private Transform _poolTrans;
        private Transform _templateTrans;
        private Transform _backgroundTrans;
        private Transform _bottomTrans;
        private Transform _contentTrans;
        private Transform _popupTrans;
        private Transform _overlayTrans;
        private Transform _foregroundTrans;
        private Transform _worldTrans;

        private void InitHier()
        {
            _backgroundTrans = transform.Find("Background/_Trans_Background");
            _bottomTrans = transform.Find("Bottom/_Trans_Bottom");
            _contentTrans = transform.Find("Content/_Trans_Content");
            _popupTrans = transform.Find("Popup/_Trans_Popup");
            _overlayTrans = transform.Find("Overlay/_Trans_Overlay");
            _foregroundTrans = transform.Find("Foreground/_Trans_Foreground");
        }
        // Hier Components End

        // Private Methods Begin
        private void SetSafeArea()
        {

        }

        private void InitCanvas()
        {
            //_poolTrans.parent.GetComponent<Canvas>().worldCamera = GlobalHierarchy.MainCamera;
            //_templateTrans.parent.GetComponent<Canvas>().worldCamera = GlobalHierarchy.MainCamera;
            //_backgroundTrans.parent.GetComponent<Canvas>().worldCamera = GlobalHierarchy.MainCamera;
            //_bottomTrans.parent.GetComponent<Canvas>().worldCamera = GlobalHierarchy.MainCamera;
            //_contentTrans.parent.GetComponent<Canvas>().worldCamera = GlobalHierarchy.MainCamera;
            //_popupTrans.parent.GetComponent<Canvas>().worldCamera = GlobalHierarchy.MainCamera;
            //_overlayTrans.parent.GetComponent<Canvas>().worldCamera = GlobalHierarchy.MainCamera;
            //_foregroundTrans.parent.GetComponent<Canvas>().worldCamera = GlobalHierarchy.MainCamera;
        }
        // Private Methods End

        // Private Static Methods Begin
        
        // Private Static Methods End
    }
}
